<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>IP管理</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../js/element-ui-2.13.0/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="../../styles/common.css"/>
    <link rel="stylesheet" href="../../styles/index.css"/>
    <link rel="stylesheet" href="../../styles/icon/iconfont.css"/>
    <link rel="stylesheet" href="../../styles/pages.css"/>
</head>
<body>
<div class="dashboard-container" id="category-app">
    <div class="container">
        <div
                class="tableBar"
                style="display: inline-block"
        >
            <el-input
                    v-model="ip"
                    placeholder="请输入IP地址"
                    style="width: 250px"
                    clearable
                    @input="handleQuery"
            >
            </el-input>
            <el-select v-model="ipType" filterable placeholder="请选择IP类型" @change="handleQuery" clearable>
                <el-option
                        v-for="item in ipTypeList"
                        :key="item.value"
                        :label="item.value"
                        :value="item.value">
                </el-option>
            </el-select>
            <el-button
                    type="primary"
                    @click="addIp('ip')"
            >
                + 新增IP地址
            </el-button>
        </div>
        <el-table
                :data="tableData"
                stripe
                class="tableBox"
                @sort-change="handleSortChange"
        >
            <el-table-column
                    prop="ip"
                    label="IP地址"
                    sortable="true"
            />
            </el-table-column>
            <el-table-column
                    prop="ipType"
                    label="IP类型"
                    sortable="true"
            />
            </el-table-column>
            <el-table-column
                    prop="port"
                    label="端口号"
            />
            </el-table-column>
            <el-table-column
                    prop="version"
                    label="VOS版本"
            />
            </el-table-column>
            <el-table-column
                    label="操作"
                    width="160"
                    align="center"
            >
                <template slot-scope="scope">
                    <el-button
                            type="text"
                            size="small"
                            class="blueBug"
                            @click="editHandle(scope.row)"
                    >
                        修改
                    </el-button>
                    <el-button
                            type="text"
                            size="small"
                            class="delBut non"
                            @click="deleteHandle(scope.row.ipId)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                class="pageList"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="counts"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        ></el-pagination>
    </div>
    <el-dialog
            :title="ipParams.title"
            :visible.sync="ipParams.dialogVisible"
            width="30%"
            :before-close="handleClose"
    >
        <el-form
                class="demo-form-inline"
                label-width="100px"
                :model="ipParams"
                ref="ipParams"
        >
            <el-form-item
                    label="ip地址："
                    prop="ip"
                    :rules="[
                      { required: true, message: 'IP地址不能为空', trigger: 'blur' }]">
                <el-input
                        v-model="ipParams.ip"
                        placeholder="请输入IP地址"
                        maxlength="20"
                />
            </el-form-item>
            <el-form-item
                    label="ip类型："
                    prop="ipType"
                    :rules="[
                      { required: true, message: 'IP类型不能为空', trigger: 'blur' }]">
                <el-input
                        v-model="ipParams.ipType"
                        placeholder="请输入IP类型"
                        maxlength="20"
                />
            </el-form-item>
            <el-form-item
                    label="端口号："
                    prop="port"
                    >
                <el-input
                        v-model="ipParams.port"
                        placeholder="请输入端口号"
                        maxlength="20"
                />
            </el-form-item>
            <el-form-item
                    label="VOS版本："
                    prop="version"
                    >
                <el-input
                        v-model="ipParams.version"
                        placeholder="请输入VOS版本"
                        maxlength="20"
                />
            </el-form-item>
        </el-form>
        <span
                slot="footer"
                class="dialog-footer"
        >
        <el-button
                size="medium"
                @click="handleClose()"
        >取 消</el-button>
        <el-button
                type="primary"
                size="medium"
                @click="submitForm()"
        >确 定</el-button>
        <el-button
                v-if="action != 'edit'"
                type="primary"
                size="medium"
                class="continue"
                @click="submitForm('go')"
        > 保存并继续添加 </el-button>
      </span>
    </el-dialog>
</div>
<script src="../../js/vue.js"></script>
<script src="../../js/element-ui-2.13.0/lib/index.js"></script>
<script src="../../js/axios-0.18.0.js"></script>
<script src="../../js/request.js"></script>
<script src="../../js/query.js"></script>
<script>
    new Vue({
        el: '#category-app',
        data() {
            return {
                realName: '',
                userType: '',
                action: '',
                counts: 0,
                pageNum: 1,
                pageSize: 10,
                tableData: [],
                ipTypeList: [],
                ipType: '',
                orderByColumn: '',
                isAsc: '',
                type: '',
                ip: '',
                ipId: '',
                version: '',
                port: '',
                ipParams: {
                    'title': '添加IP地址',
                    'dialogVisible': false,
                    'ip': '',
                    'ipId': '',
                    'ipType': '',
                    'version': '',
                    'port': ''
                }
            }
        },
        computed: {},
        created() {
            this.init();
        },
        mounted() {
        },
        methods: {
            async init() {
                this.ipTypeList = []
                await queryAllIpType({
                }).then(res => {
                    res.result.forEach(item => {
                        let arr = {
                            value: item.ipType,
                            label: item.ipType
                        };
                        this.ipTypeList.push(arr);
                    });
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                });
                await queryAllIp({
                    'pageNum': this.pageNum,
                    'pageSize': this.pageSize,
                    'ip': this.ip,
                    'ipType': this.ipType,
                    'orderByColumn': this.orderByColumn,
                    'isAsc': this.isAsc,
                }).then(res => {
                    if (String(res.code) == 200) {
                        this.tableData = res.result.data
                        this.counts = Number(res.result.total)
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                });
            },
            handleQuery() {
                this.pageNum = 1;
                this.init();
            },
            // 添加
            addIp(st) {
                if (st == 'ip') {
                    this.ipParams.title = '新增IP地址'
                }
                this.action = 'add'
                this.ipParams.ip = ''
                this.ipParams.ipId = ''
                this.ipParams.ipType = ''
                this.ipParams.port = ''
                this.ipParams.version = ''
                this.ipParams.dialogVisible = true
            },
            editHandle(dat) {
                this.ipParams.title = '修改IP地址'
                this.action = 'edit'
                this.ipParams.ip = dat.ip
                this.ipParams.ipId = dat.ipId
                this.ipParams.ipType = dat.ipType
                this.ipParams.port = dat.port
                this.ipParams.version = dat.version
                this.ipParams.dialogVisible = true
            },
            // 关闭弹窗
            handleClose() {
                this.ipParams.dialogVisible = false
                this.$refs['ipParams'].resetFields()
            },
            //删除
            deleteHandle(id) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    'confirmButtonText': '确定',
                    'cancelButtonText': '取消',
                    'type': 'warning'
                }).then(() => {
                    deleteIp({'ipId': id}).then(res => {
                        if (res.code === 200) {
                            this.$message.success(res.msg)
                            this.handleQuery()
                        } else {
                            this.$message.error(res.msg || '操作失败')
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                })
            },
            //数据提交
            submitForm(st) {
                this.$refs[`ipParams`].validate((valid) => {
                    if (valid) {
                        const ipParams = this.ipParams
                        if (this.action === 'add') {
                            addIp(ipParams).then(res => {
                                if (res.code === 200) {
                                    this.$message.success(res.msg)
                                    if (!st) {
                                        this.ipParams.dialogVisible = false
                                    }
                                    this.handleQuery()
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        } else if (this.action === 'edit') {
                            updateIp(ipParams).then(res => {
                                if (res.code === 200) {
                                    this.$message.success(res.msg)
                                    if (!st) {
                                        this.ipParams.dialogVisible = false
                                    }
                                    this.handleQuery()
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        }
                    }
                })
            },
            //切换页面大小
            handleSizeChange(val) {
                this.pageSize = val
                this.init()
            },
            //换页
            handleCurrentChange(val) {
                this.pageNum = val
                this.init()
            },
            //排序
            handleSortChange(val) {
                if (val.column) {
                    this.orderByColumn = val.prop
                    this.isAsc = val.order == 'ascending' ? 'ASC' : 'DESC'
                }
                this.init()
            }
        }
    })
</script>
</body>
</html>